module.exports = (app, router, assert, utils, mongoose) => {
    const classifyOne = require('../../../model/ClassifyOne');
    const classifyTwo = require('../../../model/ClassifyTwo');
    const classifyThree = require('../../../model/ClassifyThree');
    /**添加分类 */
    router.post('/classify/add', async (req, res) => {
        const data = req.body;
        const type_id = Number(data.type_id)
        /**1是一级分类 ...*/
        let count = null;
        switch (data.rank) {
            case 1:
                /**判断是否重名 */
                count = await classifyOne.countDocuments({ name: data.name, type_id: type_id });
                assert(!count, 200, { code: 40002, message: '一级分类名称已存在' });
                if (!data.edit_id) {
                    /**插入数据 */
                    await classifyOne.create(data, (err) => {
                        utils.result(err, res, '参数错误', '保存成功');
                    })
                }
                /**修改数据 */
                else {
                    await classifyOne.updateOne({ _id: data.edit_id }, data, (err) => {
                        utils.result(err, res, '修改参数错误', '修改成功');
                    })
                }
                break;
            case 2:
                count = await classifyTwo.countDocuments({ name: data.name, classify_one_id: Object(data.classify_one_id), type_id: type_id });
                assert(!count, 200, { code: 40002, message: '二级分类名称已存在' });
                if (!data.edit_id) {
                    /**插入数据 */
                    await classifyTwo.create(data, (err) => {
                        utils.result(err, res, '参数错误', '保存成功');
                    })
                }
                /**修改数据 */
                else {
                    await classifyTwo.updateOne({ _id: data.edit_id }, data, (err) => {
                        utils.error(err, res, '修改参数错误');
                    })
                    await classifyThree.updateMany({ classify_two_id: Object(data.edit_id) }, { $set: { classify_one_id: data.classify_one_id } }, (err) => {
                        utils.result(err, res, '修改参数错误', '修改成功');
                    })
                }
                break;
            case 3:
                count = await classifyThree.countDocuments({ name: data.name, classify_one_id: Object(data.classify_one_id), classify_two_id: Object(data.classify_two_id), type_id: type_id });
                assert(!count, 200, { code: 40002, message: '三级级分类名称已存在' });
                if (!data.edit_id) {
                    /**插入数据 */
                    await classifyThree.create(data, (err) => {
                        utils.result(err, res, '参数错误', '保存成功');
                    })
                }
                /**修改数据 */
                else {
                    await classifyThree.updateOne({ _id: data.edit_id }, data, (err) => {
                        utils.result(err, res, '修改参数错误', '修改成功');
                    })
                }
                break;
        }
    })
    /**添加三级分类参数和示例 */
    router.post('/classify/detail', async (req, res) => {
        let data = req.body;
        await classifyThree.updateOne({ _id: data.id }, data, (err) => {
            utils.result(err, res, '添加详细失败', '添加成功');
        })
    })
    /**删除分类 */
    router.get('/classify/del', async (req, res) => {
        let params = req.query;
        switch (params.delId) {
            case '1':
                // console.log(123);
                await classifyOne.deleteOne({ _id: params._id }, (err) => {
                    utils.error(err, res, "一级删除失败");
                })
                await classifyTwo.deleteMany({ classify_one_id: Object(params._id) }, (err) => {
                    utils.error(err, res, "二级删除失败");
                })
                await classifyThree.deleteMany({ classify_one_id: Object(params._id) }, (err) => {
                    utils.error(err, res, "三级删除失败");
                })
                utils.success(res, "删除成功");
                break;
            case '2':
                await classifyTwo.deleteOne({ _id: params._id }, (err) => {
                    utils.error(err, res, "二级删除失败");
                })
                await classifyThree.deleteMany({ classify_two_id: Object(params._id) }, (err) => {
                    utils.error(err, res, "三级删除失败");
                })
                utils.success(res, "删除成功");
                break;
            case '3':
                await classifyThree.deleteOne({ _id: params._id }, (err) => {
                    utils.result(err, res, '三级删除失败', '删除成功');
                })
                break;
        }
    })
    /**修改分类状态，是停用还是启用 */
    router.get('/classify/state', async (req, res) => {
        const params = req.query;
        switch (params.rank) {
            case '1':
                await classifyOne.updateOne({ _id: params._id }, { $set: { state: params.state == "true" ? 1 : 0 } }, (err) => {
                    utils.result(err, res, '更新失败参数传错', '更新成功');
                })
                break;
            case '2':
                await classifyTwo.updateOne({ _id: params._id }, { $set: { state: params.state == "true" ? 1 : 0 } }, (err) => {
                    utils.result(err, res, '更新失败参数传错', '更新成功');
                })
                break;
            case '3':
                await classifyThree.updateOne({ _id: params._id }, { $set: { state: params.state == "true" ? 1 : 0 } }, (err) => {
                    utils.result(err, res, '更新失败参数传错', '更新成功');
                })
                break;
        }
    })
    /**查询一级分类下的二级分类 */
    router.get('/classify/query', async (req, res) => {
        const params = req.query;
        const type_id = Number(params.type_id);
        switch (params.rank) {
            case '3'://到第三级的时候拉到第一级第二级要读取过值
                await classifyTwo.find({ classify_one_id: Object(params.classify_one_id), type_id: type_id, state: 1 }, { name: 1 }, (err, docs) => {
                    utils.result(err, res, '查询二级出错', '查询二级分类成功', docs);
                })
                break;
            default:
                await classifyOne.find({ type_id: type_id, state: 1 }, { name: 1 }, (err, docs) => {
                    utils.result(err, res, '查询一级出错', '查询一级分类成功', docs);
                })
                break;
        }
    })
    /**查询列表 */
    router.get('/classify/list', async (req, res) => {
        const params = req.query;
        const type_id = Number(params.type_id);//1、jquery 2、javascript 3、vue
        const rank = params.rank;//查询几级分类菜单
        const searchData = JSON.parse(params.searchData)
        switch (rank) {
            case '1':
                /**联表查询 */
                await classifyOne.aggregate([
                    {
                        $lookup: {
                            from: 'classify_two',//表名
                            localField: '_id',//表1的_id等于表2的classify_one_id
                            foreignField: 'classify_one_id',
                            as: 'classify_two'
                        },
                    },
                    {
                        $lookup: {
                            from: 'classify_three',
                            localField: '_id',
                            foreignField: 'classify_one_id',
                            as: 'classify_three'
                        },
                    },
                    { $sort: { updated: -1 } },//排序
                    { $match: { 'type_id': type_id, name: { $regex: searchData.name } } },//条件匹配。只满足条件的文档才能进入下 一阶段  模糊查询等
                    { $project: { name: 1, state: 1, "updated": 1, 'classify_two.name': 1, 'classify_two._id': 1, 'classify_three.name': 1, 'classify_three._id': 1 } },
                ], async (err, docs) => {
                    utils.error(err, res, "联表查询出错");
                    /**查询一级分类总数 */
                    await classifyOne.countDocuments({ type_id: type_id }, async (err, total) => {
                        utils.result(err, res, '查询一级分类总数出错', '查询列表成功', { total: total, list: docs });
                    });
                })
                break;
            case '2':
                /**根据一级下拉搜索 */
                if (searchData.classifyOneId != '') {
                    await classifyTwo.aggregate([
                        {
                            $lookup: {
                                from: 'classify_one',
                                localField: 'classify_one_id',
                                foreignField: '_id',
                                as: 'classify_one'
                            },
                        },
                        {
                            $lookup: {
                                from: 'classify_three',
                                localField: '_id',
                                foreignField: 'classify_two_id',
                                as: 'classify_three'
                            },
                        },
                        { $sort: { 'classify_one.name': 1 } },//排序
                        // "classify_one_id" : ObjectId("6041fcaa1faeda614034ea58")
                        { $match: { 'type_id': type_id, "classify_one_id": mongoose.Types.ObjectId(searchData.classifyOneId), name: { $regex: searchData.name } } },
                        { $project: { name: 1, state: 1, "updated": 1, 'classify_one.name': 1, 'classify_one._id': 1, 'classify_three.name': 1 } },
                    ], async (err, docs) => {
                        utils.error(err, res, "联表查询出错");
                        /**查询二级分类总数 */
                        await classifyTwo.countDocuments({ type_id: type_id }, async (err, total) => {
                            utils.result(err, res, '查询一级分类总数出错', '查询列表成功', { total: total, list: docs });
                        });
                    })
                    return
                }
                /**正常搜索 */
                await classifyTwo.aggregate([
                    {
                        $lookup: {
                            from: 'classify_one',
                            localField: 'classify_one_id',
                            foreignField: '_id',
                            as: 'classify_one'
                        },
                    },
                    {
                        $lookup: {
                            from: 'classify_three',
                            localField: '_id',
                            foreignField: 'classify_two_id',
                            as: 'classify_three'
                        },
                    },
                    { $sort: { 'classify_one.name': 1 } },//排序
                    // "classify_one_id" : ObjectId("6041fcaa1faeda614034ea58")
                    { $match: { 'type_id': type_id, name: { $regex: searchData.name } } },
                    { $project: { name: 1, state: 1, "updated": 1, 'classify_one.name': 1, 'classify_one._id': 1, 'classify_three.name': 1 } },
                ], async (err, docs) => {
                    utils.error(err, res, "联表查询出错");
                    /**查询二级分类总数 */
                    await classifyTwo.countDocuments({ type_id: type_id }, async (err, total) => {
                        utils.result(err, res, '查询一级分类总数出错', '查询列表成功', { total: total, list: docs });
                    });
                })
                break;
            case '3':
                // {
                //   name: '',
                //   classifyOneId: '60432f4ed2dc894a3899a33f',
                //   classifyTwoId: ''
                // }
                /**根据一级菜单查询 */
                if (searchData.classifyOneId != '' && searchData.classifyTwoId == '') {
                    await classifyThree.aggregate([
                        {
                            $lookup: {
                                from: 'classify_one',
                                localField: 'classify_one_id',
                                foreignField: '_id',
                                as: 'classify_one'
                            },
                        },
                        {
                            $lookup: {
                                from: 'classify_two',
                                localField: 'classify_two_id',
                                foreignField: '_id',
                                as: 'classify_two'
                            },
                        },
                        { $sort: { 'classify_one.name': 1 } },//排序
                        { $match: { 'type_id': type_id, name: { $regex: searchData.name }, "classify_one_id": mongoose.Types.ObjectId(searchData.classifyOneId), } },
                        { $project: { name: 1, state: 1, updated: 1, summarize: 1, argumentsArray: 1, demo: 1, 'classify_one.name': 1, 'classify_one._id': 1, 'classify_two.name': 1, 'classify_two._id': 1 } },
                    ], async (err, docs) => {
                        utils.error(err, res, "联表查询出错");
                        /**查询二级分类总数 */
                        await classifyThree.countDocuments({ type_id: type_id }, async (err, total) => {
                            utils.result(err, res, '查询一级分类总数出错', '查询列表成功', { total: total, list: docs });
                        });
                    })
                    return
                }
                if (searchData.classifyOneId != '' && searchData.classifyTwoId != '') {
                    await classifyThree.aggregate([
                        {
                            $lookup: {
                                from: 'classify_one',
                                localField: 'classify_one_id',
                                foreignField: '_id',
                                as: 'classify_one'
                            },
                        },
                        {
                            $lookup: {
                                from: 'classify_two',
                                localField: 'classify_two_id',
                                foreignField: '_id',
                                as: 'classify_two'
                            },
                        },
                        { $sort: { 'classify_one.name': 1 } },//排序
                        { $match: { 'type_id': type_id, name: { $regex: searchData.name }, "classify_one_id": mongoose.Types.ObjectId(searchData.classifyOneId), "classify_two_id": mongoose.Types.ObjectId(searchData.classifyTwoId) } },
                        { $project: { name: 1, state: 1, updated: 1, summarize: 1, argumentsArray: 1, demo: 1, 'classify_one.name': 1, 'classify_one._id': 1, 'classify_two.name': 1, 'classify_two._id': 1 } },
                    ], async (err, docs) => {
                        utils.error(err, res, "联表查询出错");
                        /**查询二级分类总数 */
                        await classifyThree.countDocuments({ type_id: type_id }, async (err, total) => {
                            utils.result(err, res, '查询一级分类总数出错', '查询列表成功', { total: total, list: docs });
                        });
                    })
                    return
                }
                /**正常搜索 */
                await classifyThree.aggregate([
                    {
                        $lookup: {
                            from: 'classify_one',
                            localField: 'classify_one_id',
                            foreignField: '_id',
                            as: 'classify_one'
                        },
                    },
                    {
                        $lookup: {
                            from: 'classify_two',
                            localField: 'classify_two_id',
                            foreignField: '_id',
                            as: 'classify_two'
                        },
                    },
                    { $sort: { 'classify_one.name': 1 } },//排序
                    { $match: { 'type_id': type_id, name: { $regex: searchData.name } } },
                    { $project: { name: 1, state: 1, updated: 1, summarize: 1, argumentsArray: 1, demo: 1, 'classify_one.name': 1, 'classify_one._id': 1, 'classify_two.name': 1, 'classify_two._id': 1 } },
                ], async (err, docs) => {
                    utils.error(err, res, "联表查询出错");
                    /**查询二级分类总数 */
                    await classifyThree.countDocuments({ type_id: type_id }, async (err, total) => {
                        utils.result(err, res, '查询一级分类总数出错', '查询列表成功', { total: total, list: docs });
                    });
                })
                break;
        }
    })
}